package com.antexpress.dao;

import com.antexpress.pojo.MapCity;
import com.antexpress.pojo.MapDistrict;
import com.antexpress.pojo.MapProvince;
import com.antexpress.pojo.MapTown;
import com.antexpress.util.DataUtil;
import com.antexpress.util.PinyinUtil;
import java.util.ArrayList;
import java.util.Date;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class AreaDAOImpl implements AreaDAO{
    @Autowired  
    private SessionFactory sessionFactory;

    private Session getSession(){ 
        return sessionFactory.getCurrentSession();
    }
    
    @Override
    public ArrayList<MapProvince> getAllProvince(){
        Session session = getSession();
        Criteria crit = session.createCriteria(MapProvince.class);
        ArrayList<MapProvince> proviceList = (ArrayList<MapProvince>) crit.list();
        if(proviceList!=null)
            return proviceList;
        else
            return null;
    }
    
    @Override
    @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_UNCOMMITTED)
    public boolean addProvince(ArrayList<MapProvince> provinceList){
        Session session = getSession();
        boolean isSuccess = false;
        try{
            session.beginTransaction();
            PinyinUtil pinyin = new PinyinUtil();
            for(MapProvince province:provinceList){
                String name = province.getName();
                name = DataUtil.dealProvinceName(name);
                name = pinyin.toPinYin(name, "", PinyinUtil.Type.FIRSTUPPER);
                province.setName(name);
                session.save(province);
            }
            session.getTransaction().commit();
            session.flush();
            isSuccess = true;
        }catch(Exception e){
            e.printStackTrace();
            isSuccess = false;
        }
        return isSuccess;
    }
    
    
    @Override
    public MapProvince getProvinceById(String id) {
        Session session = getSession();
        Criteria crit = session.createCriteria(MapProvince.class);
        crit.add(Restrictions.eq("id", id));
        System.out.println("正在获取省份的id"+id);
        MapProvince province = (MapProvince) crit.list().get(0);
        return province;
    }
    
    @Override
    public ArrayList<MapCity> getCityByProvice(MapProvince provice) {
        Session session = getSession();
        Criteria crit = session.createCriteria(MapCity.class);
        crit.add(Restrictions.eq("mapProvince", provice));
        ArrayList<MapCity> cityList = (ArrayList<MapCity>) crit.list();
        return cityList;
    }

    @Override
    @Transactional(propagation=Propagation.NESTED,isolation=Isolation.READ_UNCOMMITTED)
    public boolean addCity(ArrayList<MapCity> cityList) {
        Session session = getSession();
        boolean isSuccess = false;
        session.beginTransaction();
        PinyinUtil pinyin = new PinyinUtil();
        int i = 0;
        for(MapCity city:cityList){
            String name = city.getName();
            name = DataUtil.dealCityName(name);
            name = pinyin.toPinYin(name, "", PinyinUtil.Type.FIRSTUPPER);
            city.setName(name);
            MapProvince mapProvince = this.getProvinceById(city.getMapProvince().getId());
            city.setMapProvince(mapProvince);
            session.save(city);
//            i++;
//            if(i%50==0){   
//                //每次批量把50条数据写入数据库并释放内存  
//                session.flush();  
//                session.clear();  
//            }
        }
        session.getTransaction().commit();
        session.flush();
        isSuccess = true;
        return isSuccess;
    }

    @Override
    public ArrayList<MapDistrict> getDistrictByCity(MapCity city) {
        Session session = getSession();
        Criteria crit = session.createCriteria(MapDistrict.class);
        crit.add(Restrictions.eq("mapCity", city));
        ArrayList<MapDistrict> districtList = (ArrayList<MapDistrict>) crit.list();
        return districtList;
    }

    @Override
    @Transactional(propagation=Propagation.NESTED,isolation=Isolation.READ_UNCOMMITTED)
    public boolean addDistrict(ArrayList<MapDistrict> districtList) {
        Session session = getSession();
        boolean isSuccess = false;
        session.beginTransaction();
        PinyinUtil pinyin = new PinyinUtil();
        int i = 0;
        for(MapDistrict dis:districtList){
            String name = dis.getName();
            name = DataUtil.dealDistrictName(name);
            name = pinyin.toPinYin(name, "", PinyinUtil.Type.FIRSTUPPER);
            dis.setName(name);
            MapCity city = this.getCityById(dis.getMapCity().getId());
            dis.setMapCity(city);
            session.save(dis);
//            i++;
//            if(i%50==0){   
//                //每次批量把50条数据写入数据库并释放内存  
//                session.flush();  
//                session.clear();  
//            }
        }
        session.getTransaction().commit();
        session.flush();
        isSuccess = true;
        return isSuccess;
    }

    @Override
    public ArrayList<MapTown> getTownByDistrict(MapDistrict district) {
        Session session = getSession();
        Criteria crit = session.createCriteria(MapTown.class);
        crit.add(Restrictions.eq("mapDistrict", district));
        ArrayList<MapTown> townList = (ArrayList<MapTown>) crit.list();
        return townList;
    }

    @Override
    @Transactional(propagation=Propagation.NESTED,isolation=Isolation.READ_UNCOMMITTED)
    public boolean addTown(ArrayList<MapTown> townList) {
        Session session = getSession();
        boolean isSuccess = false;
        session.beginTransaction();
        PinyinUtil pinyin = new PinyinUtil();
        int i = 0;
        for(MapTown town:townList){
            String name = town.getName();
            name = DataUtil.dealTownName(name);
            name = pinyin.toPinYin(name, "", PinyinUtil.Type.FIRSTUPPER);
            town.setName(name);
            MapDistrict dis = this.getDistrictById(town.getMapDistrict().getId());
            town.setMapDistrict(dis);
            session.save(town);
//            i++;
//            if(i%50==0){   
//                //每次批量把50条数据写入数据库并释放内存  
//                session.flush();  
//                session.clear();  
//            }
        }
        session.getTransaction().commit();
        session.flush();
        isSuccess = true;
        return isSuccess;
    }

    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public boolean addFileContentToDatabase(ArrayList<MapProvince> provinceList,
                                               ArrayList<MapCity> cityList,
                                               ArrayList<MapDistrict> districtList,
                                               ArrayList<MapTown> townList){
        Session session = getSession();
        boolean isSuccess = false;
        session.beginTransaction();
        int i = 0;
        if(!provinceList.isEmpty()){
            for(MapProvince province:provinceList){
                session.save(province);
//                i++;
//                if (i%50==0){   
//                    //每次批量把50条数据写入数据库并释放内存  
//                    session.flush();  
//                    session.clear();  
//                } 
            }
        }

        if(!cityList.isEmpty()){
            for(MapCity city:cityList){
                session.save(city);
//                i++;
//                if (i%50==0){   
//                    //每次批量把50条数据写入数据库并释放内存  
//                    session.flush();  
//                    session.clear();  
//                } 
            }
            session.flush();  
            session.clear();
        }
        if(!districtList.isEmpty()){
            for(MapDistrict district:districtList){
                session.save(district);
//                i++;
//                if (i%50==0){   
//                    //每次批量把50条数据写入数据库并释放内存  
//                    session.flush();  
//                    session.clear();  
//                } 
            }
            session.flush();  
            session.clear(); 
        }
        if(!townList.isEmpty()){
            for(MapTown town:townList){
                session.save(town);
//                i++;
//                if (i%50==0){   
//                    //每次批量把50条数据写入数据库并释放内存  
//                    session.flush();  
//                    session.clear();  
//                }
            }
            session.flush();  
            session.clear(); 
        }

        session.getTransaction().commit();
        session.flush();
        isSuccess = true;
        return isSuccess;
        
    }

    @Override
    public MapCity getCityById(String id) {
        Session session = getSession();
        Criteria crit = session.createCriteria(MapCity.class);
        crit.add(Restrictions.eq("id", id));
        MapCity city = (MapCity) crit.list().get(0);
        return city;
    }

    @Override
    public MapDistrict getDistrictById(String id) {
        Session session = getSession();
        Criteria crit = session.createCriteria(MapDistrict.class);
        crit.add(Restrictions.eq("id", id));
        MapDistrict dis = (MapDistrict) crit.list().get(0);
        return dis;
    }

    @Override
    public MapTown getTownById(String id) {
        Session session = getSession();
        Criteria crit = session.createCriteria(MapTown.class);
        crit.add(Restrictions.eq("id", id));
        MapTown town = (MapTown) crit.list().get(0);
        return town;
    }

   
    
    
    
    
}
